package com.atguigu.gmall.juc.juc;

import java.util.concurrent.*;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author 陈昆昆
 * @version 1.0
 * @date 2020/5/30 22:07
 */
public class ShutDownStop extends ThreadPoolExecutor {
    private volatile  boolean flag;
    private ReentrantLock lock = new ReentrantLock();
    Condition condition = lock.newCondition();

    public ShutDownStop(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue) {
        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);
    }

    public ShutDownStop(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory) {
        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory);
    }

    public ShutDownStop(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, RejectedExecutionHandler handler) {
        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, handler);
    }

    public ShutDownStop(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler) {
        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory, handler);
    }

    @Override
    protected void beforeExecute(Thread t, Runnable r) {
        super.beforeExecute(t, r);
        lock.lock();
        try {
            while (flag){
                condition.await();
            }
        }catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            lock.unlock();
        }

    }

    public void  start(){
        lock.lock();
        try{
            flag = false;
            condition.signalAll();
        }finally {
            lock.unlock();
        }
    }

    public void  stop(){
        lock.lock();
        try {
            flag = true;
        }finally {
            lock.unlock();
        }
    }

    public static void main(String[] args) throws InterruptedException {
        ShutDownStop shutDownStop = new ShutDownStop(10, 20, 10L, TimeUnit.SECONDS, new LinkedBlockingDeque<>());
        Runnable runnable = new Runnable() {
            @Override
            public void run() {

                System.out.println("被执行了");
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };
        for (int i = 0; i < 10000; i++) {
            shutDownStop.execute(runnable);
        }
        Thread.sleep(1500);
        shutDownStop.stop();
        System.out.println("线程池被暂停了");
        Thread.sleep(15000);
        shutDownStop.start();
        System.out.println("线程池被开始了");
//        shutDownStop.start();
//        System.out.println("线程池又开始了");
    }
}
